package com.cq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cq.excelModel.PlatformComparisonExcel;
import com.cq.excelModel.PlatformComparisonPointExcel;
import com.cq.mapper.ActivityMediaSlotAnalysisMapper;
import com.cq.pojo.PlatformComparison;
import com.cq.service.ActivityMediaSlotAnalysisService;
import com.cq.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ActivityMediaSlotAnalysisServiceImpl implements ActivityMediaSlotAnalysisService {

    @Autowired
    private ActivityMediaSlotAnalysisMapper activityMediaSlotAnalysisMapper;

    @Override
    public List<String> findContactPoint() {
        return activityMediaSlotAnalysisMapper.getContactPoint();
    }

    @Override
    public Map<String, Map<String, List<PlatformComparison>>> platformDeliveryComparison(String cidA, String cidB, String filed) {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectPlatformDeliveryComparison(cidA, cidB, filed);

        Map<String, List<PlatformComparison>> collect = list.stream().collect(Collectors.groupingBy(PlatformComparison::getCampaignName));

        HashMap<String, Map<String, List<PlatformComparison>>> map = new HashMap<>();

        collect.forEach((key, value) -> {
            Map<String, List<PlatformComparison>> collected = value.stream().collect(Collectors.groupingBy(PlatformComparison::getMedia));
            map.put(key, collected);
        });

        return map;
    }

    @Override
    public void platformDeliveryComparisonDownload(String cidA, String cidB, String filed, HttpServletResponse response) throws IOException {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectPlatformDeliveryComparison(cidA, cidB, filed);

        List<PlatformComparisonExcel> collect = list.stream()
                .map(p -> {
                    PlatformComparisonExcel excel = new PlatformComparisonExcel();
                    BeanUtils.copyProperties(p, excel);
                    return excel;
                }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("平台投放对比" + System.currentTimeMillis(), "UTF-8");

        EasyExcel.write(response.getOutputStream(), PlatformComparisonExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("多活动分媒介分平台")
                .doWrite(collect);
    }

    @Override
    public Map<String, Map<String, List<PlatformComparison>>> distributionOfSubMediaPlatforms(String cidA, String cidB, String filed) {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectDistributionOfSubMediaPlatforms(cidA, cidB, filed);

        Map<String, List<PlatformComparison>> collect = list.stream().collect(Collectors.groupingBy(PlatformComparison::getMedia));

        HashMap<String, Map<String, List<PlatformComparison>>> map = new HashMap<>();

        collect.forEach((key, value) -> {
            Map<String, List<PlatformComparison>> collect1 = value.stream().collect(Collectors.groupingBy(PlatformComparison::getCampaignName));
            map.put(key, collect1);
        });
        return map;
    }

    @Override
    public void distributionOfSubMediaPlatformsDownload(String cidA, String cidB, String filed, HttpServletResponse response) throws IOException {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectDistributionOfSubMediaPlatforms(cidA, cidB, filed);

        List<PlatformComparisonExcel> collect = list.stream()
                .map(p -> {
                    PlatformComparisonExcel excel = new PlatformComparisonExcel();
                    BeanUtils.copyProperties(p, excel);
                    return excel;
                }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("多活动分媒介分平台分布对比" + System.currentTimeMillis(), "UTF-8");

        EasyExcel.write(response.getOutputStream(), PlatformComparisonExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response,fileName))
                .sheet("多活动分媒介分平台分布对比")
                .doWrite(collect);
    }

    @Override
    public Map<String, Map<String, List<PlatformComparison>>> contactTypeComposition(String cidA, String cidB, String filed) {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectContactTypeComposition(cidA, cidB, filed);
        Map<String, List<PlatformComparison>> getMedia = list.stream().collect(Collectors.groupingBy(PlatformComparison::getMedia));

        Map<String, Map<String, List<PlatformComparison>>> map = new HashMap<>();
        getMedia.forEach((key, value) -> {
            Map<String, List<PlatformComparison>> collect = value.stream().collect(Collectors.groupingBy(PlatformComparison::getCampaignName));
            map.put(key, collect);
        });

        return map;
    }

    @Override
    public void contactTypeCompositionDownload(String cidA, String cidB, String filed, HttpServletResponse response) throws IOException {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectContactTypeComposition(cidA, cidB, filed);

        List<PlatformComparisonPointExcel> collect = list.stream().map(x -> {
            PlatformComparisonPointExcel excel = new PlatformComparisonPointExcel();
            BeanUtils.copyProperties(x, excel);
            return excel;
        }).collect(Collectors.toList());

        String fileName = URLEncoder.encode("分媒介分触点类型构成" + System.currentTimeMillis(), "UTF-8");

        EasyExcel.write(response.getOutputStream(), PlatformComparisonPointExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("分媒介分触点类型构成")
                .doWrite(collect);
    }

    @Override
    public Map<String, List<PlatformComparison>> contactDistribution(String cidA, String cidB, String filed, String media) {
        List<PlatformComparison> list = activityMediaSlotAnalysisMapper.selectContactDistribution(cidA,cidB,filed,media);

        Map<String, List<PlatformComparison>> getMedia = list.stream().collect(Collectors.groupingBy(PlatformComparison::getMedia));
        Map<String, List<PlatformComparison>> map = new HashMap<>();

        getMedia.forEach((key, value) -> {
            Map<String, List<PlatformComparison>> collect = value.stream().collect(Collectors.groupingBy(PlatformComparison::getContactPoint));
            collect.forEach((k,v) -> {
                map.put(key + "-" + k, v);
            });
        });
        return map;
    }
}
